let form = document.querySelector('form');
    let errBox = document.querySelector('.error');
    let nameInp = document.querySelector('.username');
    let pwdInp = document.querySelector('.password');

    // 后端登录函数
    async function backendLogin(username, password) {
      try {
        let { data } = await http.post('/users/login', { username, password });
        if (data.code === 1) {
          return { success: true, user: data.user };
        } else {
          // 这里可以根据后端返回的具体错误信息进行更详细的提示
          return { success: false, message: `后端登录失败，原因：${data.message}` };
        }
      } catch (error) {
        console.error('后端登录出错:', error);
        return { success: false, message: '后端登录出错，请稍后重试' };
      }
    }

    // 本地存储登录函数
    function localStorageLogin(username, password) {
      var users = JSON.parse(localStorage.getItem('users')) || [];
      for (var i = 0; i < users.length; i++) {
        if (users[i].username === username && users[i].password === password) {
          return { success: true, user: users[i] };
        }
      }
      return { success: false, message: '登录失败，请检查用户名和密码' };
    }

    form.addEventListener('submit', async function (e) {
      e.preventDefault();
      let username = nameInp.value;
      let password = pwdInp.value;
      if (!username ||!password) {
        alert('请完整填写表单');
        return;
      }
      // 先尝试后端登录
      let backendResult = await backendLogin(username, password);
      if (backendResult.success) {
        errBox.style.display = 'none';
        localStorage.setItem('token', backendResult.user.token);
        localStorage.setItem('id', backendResult.user.id);
        localStorage.setItem('currentUser', JSON.stringify(backendResult.user));
        alert('登录成功');
        window.location.href = './index.html';
      } else {
        // 后端登录失败，尝试本地存储登录
        let localStorageResult = localStorageLogin(username, password);
        if (localStorageResult.success) {
          localStorage.setItem('currentUser', JSON.stringify(localStorageResult.user));
          alert('登录成功');
          window.location.href = './index.html';
        } else {
          errBox.style.display = 'block';
          alert(localStorageResult.message);
        }
      }
    });
   